import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/apartment/api.dart";
import "package:ct_pa_majordomo/pages/user/api.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/utils/common.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:ct_pa_majordomo/widgets/tai_dialog/index.dart";
import 'package:flutter/material.dart';
import "package:flutter_easyloading/flutter_easyloading.dart";


class ApartmentSetting extends StatefulWidget {
  const ApartmentSetting({Key? key}) : super(key: key);

  @override
  State<ApartmentSetting> createState() => _ApartmentSettingState();
}

class _ApartmentSettingState extends State<ApartmentSetting> {
  Map<String, dynamic> _params = {
    "name": "",
    "phonenumber": "",
    "address": ""
  };

  void _getApartmentInfo() async {
    Map resData = await apartmentApi.getApartmentInfo();
    setState(() {
      _params = resData["data"] ?? [];
    });
    print(_params);
  }

  void _updateApartmentInfo(Map<String, dynamic> data) async {
    await apartmentApi.updateApartmentInfo(data);
    _getApartmentInfo();
  }

  // 编辑公寓名称
  void _editApartmentName() {
    String tempName = _params["name"];
    taiDialog(
        context: context,
        title: "公寓名称",
        child: TextFormField(
          autofocus: true,
          textAlign: TextAlign.center,
          decoration: const InputDecoration(
            hintText: "请输入",
            hintStyle: TextStyle(color: ThemeVars.colorTextTer),
            border: InputBorder.none,
            filled: true,
            fillColor: ThemeVars.colorBackground,
          ),
          initialValue: tempName,
          onChanged: (val) {
            tempName = val;
          },
        ),
        onConfirm: (taiContext) async {
          _updateApartmentInfo({
            "name": tempName,
            "apartmentId": _params["apartmentId"],
          });
          if (taiContext.mounted) {
            Navigator.of(taiContext).pop();
          }
        }
    );
  }

  // 编辑客服电话
  void _editServicePhone() {
    String tempPhone = _params["phonenumber"];
    taiDialog(
        context: context,
        title: "客服电话",
        child: TextFormField(
          autofocus: true,
          textAlign: TextAlign.center,
          decoration: const InputDecoration(
            hintText: "请输入",
            hintStyle: TextStyle(color: ThemeVars.colorTextTer),
            border: InputBorder.none,
            filled: true,
            fillColor: ThemeVars.colorBackground,
          ),
          initialValue: tempPhone,
          onChanged: (val) {
            tempPhone = val;
          },
        ),
        onConfirm: (taiContext) async {
          if (!CommonTools.phone(tempPhone)) {
            EasyLoading.showToast("请输入正确的手机号码");
            return;
          }
          _updateApartmentInfo({
            "phonenumber": tempPhone,
            "apartmentId": _params["apartmentId"],
          });
          if (taiContext.mounted) {
            Navigator.of(taiContext).pop();
          }
        }
    );
  }

  // 选择城市
  void _editAddress() async {
    String tempAddress = _params["address"];
    taiDialog(
        context: context,
        title: "所在城市",
        child: TextFormField(
          autofocus: true,
          textAlign: TextAlign.center,
          decoration: const InputDecoration(
            hintText: "请输入",
            hintStyle: TextStyle(color: ThemeVars.colorTextTer),
            border: InputBorder.none,
            filled: true,
            fillColor: ThemeVars.colorBackground,
          ),
          initialValue: tempAddress,
          onChanged: (val) {
            tempAddress = val;
          },
        ),
        onConfirm: (taiContext) async {
          _updateApartmentInfo({
            "address": tempAddress,
            "apartmentId": _params["apartmentId"],
          });
          if (taiContext.mounted) {
            Navigator.of(taiContext).pop();
          }
        }
    );
  }

  @override
  void initState() {
    super.initState();
    _getApartmentInfo();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "公寓设置",
      child: ListView(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
          children: [
            MenuBlock(
              items: [
                MenuBlockItem(
                  text: '公寓名称',
                  suffix: Text(_params["name"]),
                  onTap: _editApartmentName,
                ),
                MenuBlockItem(
                  text: '客服电话',
                  suffix: Text(_params["phonenumber"]),
                  onTap: _editServicePhone,
                ),
                MenuBlockItem(
                  text: '所在城市',
                  suffix: Text(_params["address"]),
                  onTap: _editAddress,
                ),
              ],
            ),
          ],
        ),
    );
  }
}
